import urllib.request
def download1(url):
    try:
        html= urllib.request.urlopen(url).read()
    except  urllib.request.URLError as e:
        print('Downloading error:',e.reason)
        html=None
    return html
#print(download('http://www.inconcn.com/'))


def download2(url,num_reload=5):
    try:
        html=urllib.request.urlopen(url).read()
    except urllib.request.URLError as e:
        print('Downloading error:',e.reason)
        html = None
        if num_reload>0 and ( hasattr(e,'code') and 500<=e.code<=600 ):
            return download2(url,num_reload-1)
    return html
download2('http://httpstat.us/500')

def download3(url,user_agent='wswp',num_reload=5):
    headers={'User-agent':user_agent}
    request=urllib.request.Request(url,headers=headers)
    try:
        html=urllib.request.urlopen(request).read()
    except urllib.request.URLError as e:
        print('Downloading error:',e.reason)
        html = None
        if num_reload>0 and ( hasattr(e,'code') and 500<=e.code<=600 ):
            return download3(url,user_agent,num_reload-1)
    return html

download3('http://httpstat.us/500')